package com.codingInn.web.controller;

import com.codingInn.web.pojo.User;
import com.codingInn.web.validator.UserValidator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FlashMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

@Controller
public class GoController implements EnvironmentAware {
	private final Log logger = LogFactory.getLog(GoController.class);

	private Environment environment = null;

	@Autowired
	private UserValidator validator;

	@InitBinder
	private void initBinder(WebDataBinder binder){
		binder.addValidators(validator);
	}

	//处理HEAD类型的"/"请求
	@RequestMapping(value = {"/"},method = {RequestMethod.HEAD})
	public String head(){
		return "go";
	}

	//处理get类型的“index”和"/"请求
	@RequestMapping(value = {"/go","/"},method = {RequestMethod.GET})
	public String index(Model model) throws Exception{
		logger.info("=========processed by index=======");
		model.addAttribute("msg","Go GO GO");


		return "go";
//		return "redirect:userlist";
	}

	@Override
	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}

	@RequestMapping(value = "/submit",method = RequestMethod.POST)
	public String submit(HttpServletRequest request, RedirectAttributes attr) throws IOException{
		String orderId = request.getParameter("orderId");
		((FlashMap)((ServletRequestAttributes)(RequestContextHolder.getRequestAttributes())).getRequest().getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE)).put("name","aaa");
		attr.addFlashAttribute("ordersId","xxx");
		attr.addAttribute("local","zh-cn");
		((FlashMap)(request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE))).put("aaa","bbb");
		return "redirect:showorders";
	}

	@RequestMapping(value = "/showorders",method = RequestMethod.GET)
	public ModelAndView showOrders(HttpServletRequest request,Model modle){
		ModelAndView mv = new ModelAndView();//数据模型和视图
		Object attribute = request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE);
		mv.setViewName("go");
		return mv;
	}

//	@GetMapping("/tudou")
//	public String solveTudou(Model model){
//		model.addAttribute("info","tudou");
//		return "tudou:go";
//	}

	@RequestMapping(value = "/signup",method = RequestMethod.POST)
	public String signup(@Valid User user, BindingResult br, RedirectAttributes ra) throws Exception{
		ra.addFlashAttribute("user",user);
		return "user";
	}

	@RequestMapping(value = "/error")
	public String produceError(@RequestParam String user,@RequestParam Integer i){
		int ii =  1 / i;
		return "go";
	}

}
